package com.leads.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leads.entity.CrmLeads;
import com.leads.mapper.CrmLeadsMapper;
import com.leads.service.ICrmLeadsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;

import java.util.Date;

import static com.ruoyi.common.core.utils.uuid.IdUtils.simpleUUID;

/**
 * <p>
 * 线索表 服务实现类
 * </p>
 *
 * @author rick
 * @since 2022-12-13 11:23:15
 */
@Service
public class CrmLeadsServiceImpl extends ServiceImpl<CrmLeadsMapper, CrmLeads> implements ICrmLeadsService {

    //新增线索
    @Override
    public void addLeads(CrmLeads crmLeads,boolean isExcel) {
        String batchId = StringUtils.isNotEmpty(crmLeads.getBatchId()) ? crmLeads.getBatchId() : simpleUUID();
        boolean faSace = false;
        if (StringUtils.isEmpty(crmLeads.getEmail())) {
            crmLeads.setEmail(null);
        }
        //修改下次联系时间,待办事项需要提醒,需要同步最后跟进时间
        if (crmLeads.getNextTime()!=null){
            crmLeads.setLastTime(new Date());
        }
        if (crmLeads.getLeadsId() != null) {
            crmLeads.setCustomerId(0);
            crmLeads.setUpdateTime(new Date());
            updateById(crmLeads);
            //查询一次保存es,因为有些字段没有保存es会出现null
            crmLeads = getById(crmLeads.getLeadsId());
        }else {
            crmLeads.setCreateTime(cn.hutool.core.date.DateUtil.date());
            crmLeads.setUpdateTime(DateUtil.date());
            crmLeads.setCreateUserId((Long) StpUtil.getLoginId());
            crmLeads.setIsTransform(0);
            if(!isExcel){
                crmLeads.setFollowup(0);
            }
            crmLeads.setCreateUserId((Long) StpUtil.getLoginId());
            if (crmLeads.getOwnerUserId() == null) {
                crmLeads.setOwnerUserId((Long) StpUtil.getLoginId());
            }
            crmLeads.setBatchId(batchId);
            save(crmLeads);
        }
    }
}
